package com.sixks.app.mvc;

import com.sixks.app.dao.OrderDao;
import com.sixks.app.domain.sku.ClothesSKU;
import com.sixks.app.domain.sku.Product;
import com.sixks.app.domain.sku.ProductHistory;
import com.sixks.app.domain.sku.SKUShow;
import com.sixks.app.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author colin
 * @date: 16/03/2014
 * @time: 14:30
 */
@Controller
@RequestMapping("/sku")
public class SKUController {

    @Autowired
    private OrderDao orderDao;

    @RequestMapping(method = RequestMethod.GET)
    public String indexGet(HttpServletRequest request, ModelMap model) {

        return "sku";
    }
    @RequestMapping(method = RequestMethod.POST)
    public String index(HttpServletRequest request, ModelMap model) {

        if(StringUtils.isBlank(request.getParameter("dateRange"))){
            model.addAttribute("error", "日期范围必须选择");
            return "sku";
        }else{
            model.put("dateRange",request.getParameter("dateRange"));
        }
        String[] range = request.getParameter("dateRange").split("-");

        Date start = DateUtil.parseStr2Date(range[0].trim(), "yyyy/MM/dd");
        Date end = DateUtil.parseStr2Date(range[1].trim(), "yyyy/MM/dd");
        end = DateUtil.getDateAfterDays(end, 1);
        /**
         * 相隔天数
         */
        long days = DateUtil.getDistanceDays(start, end);
        if(days < 0){
            model.addAttribute("error", "日期范围:结束日期必须大于起始日期");
            return "sku";
        }
        if(days > 30){
            model.addAttribute("error", "日期范围最好小于30天");
            return "sku";
        }

        String prodId = request.getParameter("productId");
        List<ClothesSKU> skus = this.orderDao.loadSKUByDate(start, end, prodId);

        Map<Integer, ProductHistory> historyMap = new TreeMap<Integer, ProductHistory>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });
        for (ClothesSKU sku : skus) {
            ProductHistory history = new ProductHistory();
            if (historyMap.containsKey(sku.getProductId())) {
                history = historyMap.get(sku.getProductId());
            } else {
                historyMap.put(sku.getProductId(), history);
            }
            Product product = new Product();
            if (history.contains(sku.getBelongsTo().getCreate())) {
                product = history.get(sku.getBelongsTo().getCreate());
            }
            product.setId(sku.getProductId());
            product.addSKU(sku);
            history.add(sku.getBelongsTo().getCreate(), product);
        }
        //
        List<Date> dates = new ArrayList<Date>();
        for (int i = 0; i < days; i++) {
            dates.add(DateUtil.getDateAfterDays(start, i));
        }
        model.addAttribute("hmap", historyMap);
        model.addAttribute("dates", dates);
        return "sku";
    }

    public String query(HttpServletRequest request, ModelMap mode) {

        return "";
    }
}
